package com.tiza.xiaoyoubao;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;

import com.tianze.library.base.BaseActivity;
import com.tianze.library.base.BaseFragment;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.ui.fragment.MainFragment;
import com.tiza.xiaoyoubao.ui.fragment.MyNewsFragment;
import com.tiza.xiaoyoubao.ui.fragment.OneselfCenterFragment;
import com.tiza.xiaoyoubao.ui.fragment.TaskFragment;
import com.tiza.xiaoyoubao.view.ShadeView;

import org.simple.eventbus.EventBus;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {
    @BindView(R.id.sv_homePage)
    ShadeView sv_homePage;
    @BindView(R.id.sv_task)
    ShadeView sv_task;
    @BindView(R.id.sv_complainCenter)
    ShadeView sv_complainCenter;
    @BindView(R.id.sv_myNews)
    ShadeView sv_myNews;
    @BindView(R.id.sv_oneself_info)
    ShadeView sv_oneself_info;
    private List<ShadeView> tabIndicators;
    private long mLastExitTime;
    private static FragmentManager fm;
    private static WeakReference<Fragment> mFragment;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected int getTextColor() {
        return getResources().getColor(R.color.title2);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ((AppBarLayout) mToolbar.getParent()).setElevation(0f);
        }
        mToolbar.setBackgroundResource(R.color.white);
        EventBus.getDefault().register(this);
        fm = getSupportFragmentManager();
        initView();
    }

    private void initView() {
        //TODO(获取IMEI)
        String imei =((TelephonyManager) mActivity.getSystemService(TELEPHONY_SERVICE)).getDeviceId();
        Log.i("IMEI","IMEI是："+imei);
        showFragment(new MainFragment());
        hideLeftTextAction();
        setToolbarTitle("小优宝");
        setRightActionResource(R.mipmap.sign_in);
        setRightActionListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UIHelper.showSignIn(mActivity);
            }
        });
        tabIndicators = new ArrayList<>();
        tabIndicators.add(sv_homePage);
        tabIndicators.add(sv_task);
        tabIndicators.add(sv_complainCenter);
        tabIndicators.add(sv_myNews);
        tabIndicators.add(sv_oneself_info);
        sv_homePage.setIconAlpha(1.0f);
    }


    @OnClick({
            R.id.sv_homePage,
            R.id.sv_task,
            R.id.sv_complainCenter,
            R.id.sv_myNews,
            R.id.sv_oneself_info,
    })
    public void onClick(View view) {
        resetTabsStatus();
        switch (view.getId()) {
            case R.id.sv_homePage:
                sv_homePage.setIconAlpha(1.0f);
                showFragment(new MainFragment());
                setRightActionResource(R.mipmap.sign_in);
                break;
            case R.id.sv_task:
                sv_task.setIconAlpha(1.0f);
                showFragment(new TaskFragment());
                hideRightImageAction();
                break;
            case R.id.sv_complainCenter:
                sv_complainCenter.setIconAlpha(1.0f);
                UIHelper.showComplain(mActivity);
                hideRightImageAction();
//                //showFragment(new ComplainCenterFragment());
//                showFragment(new ComplainFragment());
                break;
            case R.id.sv_myNews:
                sv_myNews.setIconAlpha(1.0f);
                showFragment(new MyNewsFragment());
                hideRightImageAction();
                break;
            case R.id.sv_oneself_info:
                sv_oneself_info.setIconAlpha(1.0f);
                showFragment(new OneselfCenterFragment());
                hideRightImageAction();
                break;
            default:
                break;
        }

    }

    /**
     * 重置Tab状态
     */
    private void resetTabsStatus() {
        for (int i = 0; i < tabIndicators.size(); i++) {
            tabIndicators.get(i).setIconAlpha(0);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - mLastExitTime < 2000) {
            super.onBackPressed();
        } else {
            mLastExitTime = System.currentTimeMillis();
            toast("再按一次退出");
        }
    }

    public static void showFragment(Fragment fragment) {
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.content_main, fragment);
        transaction.commit();
        mFragment = new WeakReference<>(fragment);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (mFragment != null && mFragment.get() != null && mFragment.get() instanceof BaseFragment) {
            BaseFragment bf = (BaseFragment) mFragment.get();
            bf.onActivityResult(requestCode, resultCode, data);
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}
